<?php

function render_begin($title) {
?>
<!doctype html>
<html>
<head>
<title>Notes<?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link type="text/css" rel="stylesheet" media="all" href="/n/style.css"/>
<link type="image/x-icon" rel="shortcut icon" href="/n/favicon.ico"/>
</head>
<body>
<?php
}



function render_profile($user) {
?>
<div id="profile">
<div class="firstlastname"><?php echo $user['first_name'].' '.$user['last_name']; ?></div>
<div class="username"><?php echo $user['username']; ?></div>
<a href="/n/?logout">log out</a>
</div>
<?php
}



function render_page_begin() {
?>
<div id="page">
<?php
}



function render_header() {
?>
<div id="header">
<div id="logo">Notes</div>
<ul>
<li><a href="/n/">Notes</a></li>
<li><a href="/n/friends">Friends</a></li>
<li><a href="/n/search">Search</a></li>
<li><a href="/n/settings">Settings</a></li>
</ul>
</div>
<?php
}



function render_edit_notes($user, $edit, $edit_id) {
?>
<div id="main"><div id="content">
<form method="POST">
<input id="newnotesubmit" type="submit" value="Edit note" tabindex="3"/>
<span id="newnotepublictxt">public</span><input id="newnotepublic" name="newnotepublic" type="checkbox" tabindex="4" <?php if (is_public_note($user, $edit_id)) echo 'checked'; ?>/>
<input id="newnotesubject" name="newnotesubject" type="text" tabindex="1" placeholder="subject" value="<?php echo htmlspecialchars($edit[0]['subject']); ?>"/><br/>
<textarea id="newnotebody" name="newnotebody" rows="5" tabindex="2" placeholder="note"><?php echo htmlspecialchars($edit[0]['body']); ?></textarea><br/>
</form>
<?php
$notes = load_note($user, $edit_id);
foreach ($notes as $note) {
?><div class="note"><div class="time">
<?php echo note_time($note['time']);?><br/>
</div><div class="text">
<?php if (!empty($note['subject'])) { ?><div class="subject"><?php echo augment_hrefs(htmlspecialchars($note['subject']));?></div><?php } ?>
<div class="body"><?php echo augment_hrefs(htmlspecialchars($note['body']));?></div></div></div><?php
}
?>
</div></div>
<?php
}



function render_all_notes($user, $settings) {
?>
<div id="main"><div id="content">
<form method="POST">
<input id="newnotesubmit" type="submit" value="Add note" tabindex="3"/>
<span id="newnotepublictxt">public</span><input id="newnotepublic" name="newnotepublic" type="checkbox" tabindex="4" <?php if ($settings['default_public'] == 1) echo 'checked'; ?>/>
<input id="newnotesubject" name="newnotesubject" type="text" tabindex="1" placeholder="subject"/><br/>
<textarea id="newnotebody" name="newnotebody" rows="5" tabindex="2" placeholder="note"></textarea><br/>
</form>
<?php
$notes = load_all_notes($user);
foreach ($notes as $note) {
?><div class="note"><div class="time">
<?php echo note_time($note['time']);?><br/>
<?php if (isset($note['public'])) { ?><b>public</b><br/><?php } ?>
<?php if ($note['edits'] > 1) { echo $note['edits']; ?>&nbsp;edits<br/><?php } ?>
<span class="edit"><a href="/n/?vi=<?php echo encode_time($note['time']); ?>">edit</a></span><br/>
<span class="delete"><a href="/n/?rm=<?php echo encode_time($note['time']); ?>">del</a></span>
</div><div class="text">
<?php if (!empty($note['subject'])) { ?><div class="subject"><?php echo augment_hrefs(htmlspecialchars($note['subject']));?></div><?php } ?>
<div class="body"><?php echo augment_hrefs(htmlspecialchars($note['body']));?></div></div></div><?php
}
?>
</div></div>
<?php
}



function render_footer() {
?>
<div id="footer">
<ul>
<li><a href="/n/about">About</a></li>
<li><a href="/n/feedback">Feedback</a></li>
</ul>
</div>
<?php
}



function render_page_end() {
?>
</div>
<?php
}



function render_end() {
?>
</body>
</html>
<?php
}



function render_search($user, $query, $results = false) {
?>
<div id="search">
<form>
<input id="searchsubmit" type="submit" value="Search" tabindex="2"/>
<input id="searchinput" name="s" type="text" tabindex="1" value="<?php echo htmlspecialchars($query); ?>"/><br/>
</form>
<?php
if ($results !== false) {
?>
<ol id="searchresults">
<?php
foreach ($results as $note_id => $result) {
?><li class="searchnode"><?php
    ?><div class="searchnodeinfo"><?php
    echo note_time(decode_time($note_id));
    ?><br/><span class="searchnodeedit"><a href="/n/?vi=<?php echo $note_id; ?>">edit</a></span><br/><?php
    ?></div><?php
    $existsmainres = isset($result[$note_id]);
    if ($existsmainres) {
        ?><div class="searchmainresult"><?php
        $note = load_note($user, $note_id);
        $note = $note[0];
        if (!empty($note['subject'])) { ?><div class="subject"><?php
            echo highlight_substring_augmenting_hrefs(htmlspecialchars($note['subject']), $query);?></div><?php
        } ?><div class="body"><?php
        echo highlight_substring_augmenting_hrefs(htmlspecialchars($note['body']), $query);?></div><?php
        ?><div class="clear"></div></div><?php
    }
    
    $resultsinedits = 0;
    $edits = 0;
    foreach ($result as $edit_id => $somearray) if ($edit_id != $note_id) {
        $edits++;
        $resultsinedits += count($somearray);
    }
    if ((!$existsmainres && !empty($result)) || ($existsmainres && count($result) > 1)) {
        ?><div class="searchmoreresults"><span class="searchmoreresultscaption">there are <?php
        if ($existsmainres) echo "also ";
        echo $resultsinedits; ?> result<?php if ($resultsinedits > 1) echo "s";?> in <?php
        echo $edits; ?> edit<?php if ($edits > 1) echo "s";?></span><div class="clear"></div></div><?php
    }
?></li><?php
}
?>
</ol>
<?php
}
?>
</div>
<?php
}

?>
